package com.joysuch.wwyt.util;


import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ning
 * 创建于 2017年10月18日下午4:05:54
 * //TODO 表单校验工具类
 */
public class Validators {

    private static final String PHONE_PATTERN = "^1\\d{10}$";

    private static final String PASSWORD_PATTERN = "^[a-zA-Z0-9][a-zA-Z0-9.!@#$%^&*_]{3,17}$";

    private static final String POINT = ".";

    private static final String POINT_ZERO = ".0";

    private static final String MAX = "max";

    private static final String MIN = "min";

    private static final String TRANSVERSE_LINE = "-";

    public static void main(String[] args) {
        boolean validateFloorNo = validateMac("1112６５４６５３３３");
        System.out.println(validateFloorNo);
    }

    public Validators() {

    }

    /**
     * 验证不同的set集合含有相同的元素
     *
     * @return boolean
     */
    public static <T> boolean validDifferentSetHaveSameElement(Set<T> set1, Set<T> set2) {
        Set<T> sumSet = new HashSet<>();
        if (set1 == null) {
            set1 = new HashSet<>();
        }
        if (set2 == null) {
            set2 = new HashSet<>();
        }
        sumSet.addAll(set1);
        sumSet.addAll(set2);
        if (sumSet.size() < (set1.size() + set2.size())) {
            //说明有重复的元素
            return true;
        }
        return false;
    }

    public static boolean validateInt(String value) {
        try {
            Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    public static boolean validateLong(String value) {
        try {
            Long.parseLong(value);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    /**
     * validateMobileNo
     * 2016年6月17日 下午5:12:59
     *
     * @param mobNo
     * @return TODO：是否是电话号码
     */
    public static boolean validateMobileNo(String mobNo) {
        boolean result = false;
        if (StringUtil.notEmpty(mobNo)) {
            Pattern p = Pattern.compile(PHONE_PATTERN);
            result = p.matcher(mobNo).matches();
        }
        return result;
    }

    /**
     * isNumeric
     * 2016年6月17日 下午5:14:27
     *
     * @param str
     * @return TODO：判断是否为数字
     */
    public static boolean isNumeric(String str) {
        if (!StringUtil.notEmpty(str)) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNegative(String str) {
        if (!StringUtil.notEmpty(str)) {
            return false;
        }
        if (str.startsWith(TRANSVERSE_LINE)) {
            for (int i = 1; i < str.length(); i++) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * isDigit
     * 2016年12月22日 下午2:25:22
     *
     * @param str
     * @return TODO：包含是否为负数,小数
     */
    public static boolean isDigit(String str) {
        if (!StringUtil.notEmpty(str)) {
            return false;
        }

        boolean strResult = str.matches("-?[0-9]+.*[0-9]*");
        return strResult;
    }

    /**
     * isValidDate
     * 2016年6月17日 下午5:25:34
     *
     * @param str
     * @param template
     * @return TODO：验证是否是想要的日期格式
     */
    public static boolean isValidDate(String str, String template) {
        boolean convertSuccess = true;
//        //默认模板为yyyy-MM-dd HH:mm:ss
//        String pattern = DateUtil.DEFAULT_FORMAT_DATE_TIME;
//        if (StringUtil.notEmpty(pattern)) {
//            pattern = template;
//        }
//        SimpleDateFormat format = new SimpleDateFormat(pattern);
//        try {
//            //设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
//            format.setLenient(false);
//            format.parse(str);
//        } catch (ParseException e) {
//            // e.printStackTrace();
//            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
//            convertSuccess = false;
//        }
        return convertSuccess;
    }

    /**
     * @return 如果是符合邮箱格式的字符串, 返回<b>true</b>,否则为<b>false</b>
     * @des 待验证的字符串
     */
    public static boolean isEmail(String str) {
        //String regex = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}" ;
        String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return match(regex, str);
    }

    /**
     * @return 如果是符合网址格式的字符串, 返回<b>true</b>,否则为<b>false</b>
     * @des 待验证的字符串
     */
    public static boolean isHomepage(String str) {
        String regex = "http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*";
        return match(regex, str);
    }

    /**
     * @param regex 正则表达式字符串
     * @param str   要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean validatePassword(String password) {
        boolean result = false;
        if (StringUtil.notEmpty(password)) {
            Pattern p = Pattern.compile(PASSWORD_PATTERN);
            result = p.matcher(password).matches();
        }
        return result;
    }

    /**
     * @param str
     * @param minLen -1 表示无最小长度,最小(默认)为1
     * @param maxLen -1　表示无最大长度,无默认值
     * @return
     */
    public static boolean validateAlphabetNumber(String str, int minLen, int maxLen) {
        boolean result = false;
        if (StringUtil.notEmpty(str)) {
            if (minLen <= 0) {
                minLen = 1;
            }
            if (maxLen > 0 && maxLen > minLen) {
                Pattern p = Pattern.compile("^[a-zA-Z0-9]{" + minLen + "," + maxLen + "}$");
                result = p.matcher(str).matches();
            } else {
                Pattern p = Pattern.compile("^[a-zA-Z0-9]{" + minLen + ",}$");
                result = p.matcher(str).matches();
            }
        }
        return result;
    }

    /**
     * @param str
     * @param minLen -1 表示无最小长度,最小(默认)为1
     * @param maxLen -1　表示无最大长度,无默认值
     * @return
     */
    private static final String USERNAME_PATTERN = "^[a-zA-Z0-9_@.]{4,18}$";

    public static boolean validateUsername(String str) {
        boolean result = false;
        if (StringUtil.notEmpty(str)) {
            Pattern p = Pattern.compile(USERNAME_PATTERN);
            result = p.matcher(str).matches();
        }
        return result;
    }

    /**
     * 验证mac地址
     *
     * @param mac
     * @return
     */
    private static final String MAC_PATTERN = "^[A-Fa-f0-9]{12}$";

    public static boolean validateMac(String str) {
        boolean result = false;
        if (StringUtil.notEmpty(str)) {
            Pattern p = Pattern.compile(MAC_PATTERN);
            result = p.matcher(str).matches();
        }
        return result;
    }


    private static final String DEVICE_UUID_NO_LINE_PATTERN = "^[A-F0-9]{8}[A-F0-9]{4}[A-F0-9]{4}[A-F0-9]{4}[A-F0-9]{12}$";

    public static boolean validateDeviceUuidNotContainLine(String str) {
        boolean result = false;
        if (StringUtil.notEmpty(str)) {
            Pattern p = Pattern.compile(DEVICE_UUID_NO_LINE_PATTERN);
            result = p.matcher(str).matches();
        }
        return result;
    }

    private static final String DEVICE_UUID_PATTERN = "^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$";

    public static boolean validateDeviceUuid(String str) {
        boolean result = false;
        if (StringUtil.notEmpty(str)) {
            Pattern p = Pattern.compile(DEVICE_UUID_PATTERN);
            result = p.matcher(str).matches();
        }
        return result;
    }

    public static boolean validateMajorMinor(int major) {
        return major >= 0 && major <= 65535;
    }

    private static final String HEX_PATTERN = "^[A-F0-9]+$";

    public static boolean validateHex(String hex) {
        boolean result = false;
        if (StringUtil.notEmpty(hex)) {
            Pattern p = Pattern.compile(HEX_PATTERN);
            result = p.matcher(hex).matches();
        }
        return result;
    }

    private static final String FLOOR_NO_PATTERN = "^Floor[B]?[1-9][0-9]*$";

    public static boolean validateFloorNo(String floorNo) {
        boolean result = false;
        if (StringUtil.notEmpty(floorNo)) {
            Pattern p = Pattern.compile(FLOOR_NO_PATTERN);
            result = p.matcher(floorNo).matches();
        }
        return result;
    }

    private static final String DOMAIN_PATTERN = "^[A-Za-z0-9_]+(\\.[A-Za-z0-9_]+)+$";

    public static boolean validateDomain(String ip) {
        boolean result = false;
        if (StringUtil.notEmpty(ip)) {
            Pattern p = Pattern.compile(DOMAIN_PATTERN);
            result = p.matcher(ip).matches();
        }
        return result;
    }

    private static final String IP_PATTERN = "^[1-9][0-9]{0,2}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$";

    public static boolean validateIp(String ip) {
        boolean result = false;
        if (StringUtil.notEmpty(ip)) {
            Pattern p = Pattern.compile(IP_PATTERN);
            result = p.matcher(ip).matches();
        }
        return result;
    }


    private static final String HOUR_TIME_PATTERN = "[012][0-9]:[0-6][0-9]";
    /**
     * hh:mm 时间格式验证
     * @param str
     * @return
     */
    public static boolean validateHourMinFormat(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        boolean matches = str.matches(HOUR_TIME_PATTERN);

        if (matches) {
            String[] items = str.split(":");
            int hour = Integer.parseInt(items[0]);
            int min = Integer.parseInt(items[1]);
            if (hour < 0 || hour > 24) {
                return false;
            }
            if (min < 0 || min > 60) {
                return false;
            }

            if (hour == 24 && min != 0) {
                return false;
            }

            return true;

        }
        return false;
    }
}
